#include <stdio.h>
#include <stdlib.h> 
#include <string.h>
#include <sys/types.h>
#include <fcntl.h>
#include <time.h>
#include <libxml/parser.h>
#include <libxml/xpath.h>
#include <libxml/xmlreader.h>
#include <libxml/xmlregexp.h> 
 
#include "collector.h"
#include "indexer.h"
#include "xmlParser.h"
 

#define MAXROWS	  255

Matchers *APPLI1_Matchers;
int matcher_num;
char *raw_log_path; 	
char *norm_log_root_path;
char *norm_log_file_path;	
char *pipe_log_path;

/////////////////////////////////////////////////////////////////

 
void collect(char *conf_file)
{
	
	APPLI1_Matchers = Matchers_init();
 	LIBXML_TEST_VERSION

   	Fill_Matchers(conf_file);
    	
	Print_Matchers(APPLI1_Matchers);
 
 	FILE *inputFilePtr;

	int ovector[30];
	int   iReadReturn;
	long  lLineLen;
	char  *szReadLine = malloc (sizeof (*szReadLine) * 1024);
	 
	
	char *id_l = malloc (sizeof (*id_l) * 1024);
	char *timestamp = malloc (sizeof (*timestamp) * 1024);	
	int id_cmp;
	int id = 0;
	
	char *path = malloc(sizeof(*path) * 512);

	inputFilePtr = fopen(raw_log_path, "r");
	if (inputFilePtr == NULL )
  	{
    		printf("Error opening rawLog");

  	}

  	while (1)
  	{
		szReadLine[0] = '\0';
		iReadReturn = T_fgets(inputFilePtr, szReadLine);
		if (iReadReturn < 0)
    		{
      			printf("Error reading rawLog");
    		}
		lLineLen = strlen(szReadLine);
		if (lLineLen)
    		{
    			
			Matcher *current = APPLI1_Matchers->first;
 			
  			printf ("\nThe line ==> %s",szReadLine);
						
			while (current != NULL)
			{
				char *precheck = strstr((const char*) szReadLine , current->precheck);
				if ( precheck == NULL )
				{
					printf("**********Log line to store**********\n"); 						break;
				}					
				else
				{
					int is_match = is_regex_match(current->regex, ovector, szReadLine);
				 	
					id_cmp = Timestamp(  timestamp);
					if(is_match > 0 )
					{
						//printf ("-------------is matching up with the Matcher-------------*%d* \n ", current->id);
						 //for (i = 1; i < is_match; i++)
 						 //printf (" |||||%s \n", get_match_value(is_match,1, szReadLine));
						// printf (" |||||%s \n", get_match_value(3,  szReadLine));
						
						sprintf(id_l, "%d", id+id_cmp );
						printf("Log line ID : %s\n", id_l );
				
						path = index_folder_creator(current, szReadLine, ovector, is_match, norm_log_root_path );
						char *user = malloc(sizeof(*user) * 1024);
	
				 		pcre_copy_named_substring(current->regex,szReadLine, ovector, is_match, "user", user, 1024); 
						filed_file_creator(current, szReadLine, ovector, is_match, path, id_l, timestamp, user, norm_log_file_path, pipe_log_path);	
						id++;	
						free(user); 						
				
						break;
					}				
					else
					{
						//printf ("is not matching up, testing the next matcher..... \n");			
					}
					current = current->next;
				} 
			}  
		}
    		if (iReadReturn == 0)
    		{
			printf("\n*******End Of Log collect*******\n");
			break;
    		}
 
  	}

	free(raw_log_path);
	free(norm_log_root_path);
	free(norm_log_file_path);
	free(pipe_log_path);
 	free(path); 
	xmlCleanupParser();
	xmlMemoryDump();
	free(id_l);
	free(timestamp);
	free(szReadLine);
	Matchers_free(APPLI1_Matchers);
	fclose(inputFilePtr);
 

}
